package com.hexinfo.dmpro.monitor.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.hexinfo.dmpro.monitor.dao.DeleteDataDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class DeleteDataService {

    @Autowired
    private DeleteDataDao deleteDataDao;

    /**
     * 清除一个月前的数据，保留一个月内数据
     */
    public boolean deleteDataMonth(int timeMonth){
        if (timeMonth == 0){
            timeMonth = 1;
        }
        //获取偏移时间
        Date currentDate = DateUtil.parse(DateUtil.now(), "yyyy-MM-dd HH:mm:ss");
        Date monthsAgoDate = DateUtil.offsetMonth(currentDate, -timeMonth);
        String monthsAgoStr = DateUtil.format(monthsAgoDate, "yyyy-MM-dd HH:mm:ss");
        int i = deleteDataDao.deleteImpalaApp(monthsAgoStr);
        log.warn("删除impala任务条数："+i);
        int i1 = deleteDataDao.deleteYarnApp(monthsAgoStr);
        log.warn("删除yarn任务条数："+i1);
        int i2 = deleteDataDao.deleteTargetDetail(monthsAgoStr);
        log.warn("删除关键指标条数："+i2);
        int i3 = deleteDataDao.deleteServiceState(monthsAgoStr);
        log.warn("删除服务状态条数："+i3);
        int i4 = deleteDataDao.deleteRoleState(monthsAgoStr);
        log.warn("删除服务角色状态条数："+i4);
        int i5 = deleteDataDao.deleteYarnScheduler(monthsAgoStr);
        log.warn("删除yarn资源池条数："+i5);
        int i6 = deleteDataDao.deleteImpalaPool(monthsAgoStr);
        log.warn("删除impala资源池条数："+i6);
        return true;
    }
}
